package com.tangyuan.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangyuan.common.payment.AliPayImpl;
import com.tangyuan.common.payment.WxPayImpl;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.feignapis.headlines.IHeadlinesFeignClient;
import com.tangyuan.feignapis.video.IVideoFeignClient;
import com.tangyuan.model.*;
import com.tangyuan.result.oConvertUtils;
import com.tangyuan.user.mapper.UserBalanceMapper;
import com.tangyuan.user.mapper.UserPublishMapper;
import com.tangyuan.user.service.UserPublishService;
import com.tangyuan.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class UserPublishServiceImpl implements UserPublishService {
    @Autowired
    UserPublishMapper userPublishMapper;

    @Autowired
    private WxPayImpl wxPayService;

    @Autowired
    private UserBalanceMapper userBalanceMapper;

    @Autowired
    private IVideoFeignClient videoFeignClient;

    @Autowired
    private IHeadlinesFeignClient headersFeignClient;

    public static final String TANG_COIN_RECHARGE = "唐币充值";

    @Override
    public Result getMyVideo(Long myUserId,Long userId,Integer pageNum,Integer pageSize) {
        return Result.ok(new PageInfo<>(videoFeignClient.getMyVideoByUserId(myUserId,userId, pageNum, pageSize)));
    }

    @Override
    public Result getMyConcern(Long userId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserConcernVo> list = userPublishMapper.getMyconcern(userId);
        Map map = new HashMap();
        map.put("concern",new PageInfo<>(list));
        return Result.ok(map);
    }

    @Override
    public Result<Long> getMyThump(Long userId) {
        Long videoThumpNum = videoFeignClient.getVideoThumpNum(userId);
        Long articleThumpNum = headersFeignClient.getArticleThumpNumByUserId(userId);
        return Result.ok(videoThumpNum + articleThumpNum);
    }

    @Override
    public Result getMyPurse(Long userId) {
        BigDecimal mypurse = userPublishMapper.getMypurse(userId);
        Map map = new HashMap();
        if (mypurse==null){
            map.put("mypurse",0);
        }else {
            map.put("mypurse",mypurse.setScale(2).toString());
        }
        return Result.ok(map);
    }

    @Override
    public Result getMyRecharge(Long userId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserBalanceVo> list= userPublishMapper.getMyRecharge(userId);
        Map map = new HashMap();
        map.put("myrecharge",new PageInfo<>(list));
        return Result.ok(map);
    }

    @Override
    public Result getMyTrumpVideo(Long userId, Integer pageNum, Integer pageSize) {
        return Result.ok(new PageInfo<>(videoFeignClient.getMyTrumpVideo(userId,pageNum,pageSize)));
    }

    @Override
    public Result getMyCollectVideo(Long userId, Integer pageNum, Integer pageSize) {
        return Result.ok(new PageInfo<>(videoFeignClient.getMyCollectVideo(userId,pageNum,pageSize)));
    }

    @Override
    public Result getMyConcerned(Long userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<MyconcernedVo> list= userPublishMapper.getMyconcerned(userId);
        for (MyconcernedVo myconcernedVo : list){
            MyconcernedVo myconcernedVo1 = userPublishMapper.getconcern(userId,myconcernedVo.getUserId());
            if (myconcernedVo1!=null){
                myconcernedVo.setIsconcern(1);
            }else {
                myconcernedVo.setIsconcern(0);
            }
        }
        Map map = new HashMap();
        map.put("myconcerned",new PageInfo<>(list));
        return Result.ok(map);
    }

    @Override
    public Result setMyConcernMark(Long userId, Long concernedUserId,String remark) {
        userPublishMapper.setMyconcernMark(concernedUserId,remark,userId);
        return Result.ok();
    }

    @Override
    public Result setMySpecialConcern(Long userId, Long concernedUserId) {
        Integer checkNum = userPublishMapper.checkspecialconcern(userId,concernedUserId);
        //设置特别关注了 取消特别关注
        if (checkNum==1){
            userPublishMapper.removespecialconcern(userId,concernedUserId);
            return Result.ok();
        }else if
        //添加特别关注
        (checkNum==0){
            userPublishMapper.setMyspecialconcern(userId,concernedUserId);
            return Result.ok();
        }

        return Result.ok();
    }

    @Override
    public Result removeMyConcerned(Long userId, Long concernUserId) {
        userPublishMapper.removeMyconcerned(userId,concernUserId);
        return Result.ok();
    }

    @Override
    public Result searchMyConcern(Long userId, Integer pageNum, Integer pageSize,String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserConcernVo> list =  userPublishMapper.searchMyconcern(userId,name);
        Map map = new HashMap();
        map.put("concern",new PageInfo<>(list));
        return Result.ok(map);
    }

    @Override
    public Result searchMyConcerned(Long userId, Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserConcernedVo> list =  userPublishMapper.searchMyconcerned(userId,name);
        for (UserConcernedVo userConcernedVo : list) {
            MyconcernedVo getConcern = userPublishMapper.getconcern(userId, userConcernedVo.getUserId());
            if (getConcern!=null){
                userConcernedVo.setIsconcern(1);
            }else {
                userConcernedVo.setIsconcern(0);
            }
        }
        Map map = new HashMap();
        map.put("myconcerned",new PageInfo<>(list));
        return Result.ok(map);
    }

    @Override
    public Result<MyInfoMissionVo> getMyInfoMission(Long userId) {
        return Result.ok(userPublishMapper.getMyInformation(userId));
    }

    //我点赞的百家
    @Override
    public Result getMyTrumpArticle(Long userId, Integer pageNum, Integer pageSize) {
        return Result.ok(new PageInfo<>(headersFeignClient.getMyTrumpHeadlines(userId, pageNum, pageSize)));
    }

    @Override
    public Result getOtherUserArticleInfo(Long userId, Integer pageNum, Integer pageSize) {
        List<Headlines> list = headersFeignClient.getMyHeadlinesByUserId(userId, pageNum, pageSize);
        Map map = new HashMap();
        map.put("article",new PageInfo<>(list));
        return Result.ok(map);
    }

    //我收藏的文章
    @Override
    public Result getMyCollectArticle(Long userId, Integer pageNum, Integer pageSize) {
        return Result.ok(new PageInfo<>(headersFeignClient.getMyCollectArticle(userId, pageNum, pageSize)));
    }

    //获取我的唐币明细
    @Override
    public Result getMyCurrencyDetails(Long userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserBalanceVo> list= userPublishMapper.getMyCurrencyDetails(userId);
        Map map = new HashMap();
        map.put("myCurrencyDetails",new PageInfo<>(list));
        return Result.ok(map);

    }

    @Override
    @Transactional
    public Result recharge(Long userId, Integer payType, BigDecimal amount) {
        if (userId == null){
            return Result.build(null,ResultCodeEnum.LOGIN_AUTH);
        }
        if (amount.compareTo(new BigDecimal(0))<=0){
            return Result.build(null,ResultCodeEnum.INCORRECT_PAYMENT_AMOUNT);
        }

        String orderNo = "RC"+oConvertUtils.orderNo();
        UserBalance userBalance = userBalanceMapper.selectByUserId(userId);

        if (userBalance == null){
            UserBalance userBalanceNew = new UserBalance();
            userBalanceNew.setUserId(userId);
            userBalanceNew.setType(1);
            userBalanceNew.setOrderNo(orderNo);
            userBalanceNew.setUpBalance(amount);
            userBalanceNew.setBalance(amount);
            userBalanceNew.setCreateTime(new Date());
            userBalanceNew.setUpdateTime(new Date());
            userBalanceNew.setStatus(0);
            userBalanceNew.setIsDelete(0);
            userBalanceMapper.insert(userBalanceNew);
        }else {
            userBalance.setId(null);
            userBalance.setUserId(userId);
            userBalance.setType(1);
            userBalance.setOrderNo(orderNo);
            userBalance.setUpBalance(amount);
            userBalance.setBalance(userBalance.getBalance().add(amount));
            userBalance.setCreateTime(new Date());
            userBalance.setUpdateTime(new Date());
            userBalance.setStatus(0);
            userBalance.setIsDelete(0);
            userBalanceMapper.insert(userBalance);
        }

        Orders orders = new Orders();
        orders.setOrderNo(orderNo);
        orders.setPayType(payType);
        orders.setPayBalance(amount);
        orders.setShopName(TANG_COIN_RECHARGE);

        if (payType == 1) {
            // 调取微信支付
            return wxPayService.wxPay(orders);
        } else if (payType == 2) {
            // 调取支付宝支付
            return AliPayImpl.pay(orders);
        }
        return Result.build(null, ResultCodeEnum.PAY_WAY_NOT_SUPPORT);
    }
}
